package com.wipz.eoa.iphone.myworkmanagent.mywork;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wipz.eoa.iphone.BaseAction;
import com.wipz.eoa.iphone.IBaseService;
import com.wipz.eoa.iphone.businessProcess.AgencyCoreService;
import com.wipz.eoa.iphone.businessProcess.TaskTransitionService;
import com.wipz.eoa.iphone.util.Constants;
/**
 * 工作委托
 * @author shang
 * @date 2012-2-3
 */
public class WorkEntrustAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private IBaseService baseService;
	
	private IWorkEntrustService workEntrustService;
	private AgencyCoreService agencyCoreService;
	private TaskTransitionService taskTransitionService;
	private Map bxSubmit;

	public Map getBxSubmit() {
		return bxSubmit;
	}

	public void setBxSubmit(Map bxSubmit) {
		this.bxSubmit = bxSubmit;
	}

	public void setTaskTransitionService(TaskTransitionService taskTransitionService) {
		this.taskTransitionService = taskTransitionService;
	}

	public void setAgencyCoreService(AgencyCoreService agencyCoreService) {
		this.agencyCoreService = agencyCoreService;
	}

	public void setBaseService(IBaseService baseService) {
		this.baseService = baseService;
	}

	public IBaseService getBaseService() {
		return baseService;
	}

	public void setWorkEntrustService(IWorkEntrustService workEntrustService) {
		this.workEntrustService = workEntrustService;
	}

	public IWorkEntrustService getWorkEntrustService() {
		return workEntrustService;
	}
	
	/**
	 * 工作委托管理 列表
	 */
	    private List allList;

		public void setAllList(List allList) {
			this.allList = allList;
		}

		public List getAllList() {
			return allList;
		}
		//工作委托详情信息
		private Map detail;
		public void setDetail(Map detail) {
			this.detail = detail;
		}
		public Map getDetail() {
			return detail;
		}
		/**
		 * 工作委托列表
		 */
		private List query;

		public void setQuery(List query) {
			this.query = query;
		}

		public List getQuery() {
			return query;
		}
	/**
	 * 所有工作委托管理 列表
	 * @return
	 * 2012-2-1 下午06:54:08
	 */
	public String allList() {
		/*String qcontent = this.getRequest().getParameter("qcontent");
		Map<String,String>	parms = new HashMap<String,String>();
		parms.put("qcontent", qcontent);
		allList = baseService.queryList("mywork.queryAllList", parms);*/
		String qcontent = this.getRequest().getParameter("qcontent");
		try {
			String s = new String(qcontent.getBytes("iso-8859-1"),"utf-8");
			qcontent= java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if(COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		String USERID =  (String) baseService.queryOne("user.queryId", COMMITERCODE);
		Map<String,String> map = new HashMap<String,String>();
		map.put("USERID", USERID);
		map.put("qcontent", qcontent);
		allList = baseService.queryList("mywork.query", map);
		return SUCCESS;
	}
	/**
	 * 查询工作委托列表
	 * @return
	 * 2012-2-5 上午10:55:13
	 */
	public String query() {
		String qcontent = this.getRequest().getParameter("qcontent");
		try {
			String s = new String(qcontent.getBytes("iso-8859-1"),"utf-8");
			qcontent= java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if(COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		String USERID =  (String) baseService.queryOne("user.queryId", COMMITERCODE);
		Map<String,String> map = new HashMap<String,String>();
		map.put("USERID", USERID);
		map.put("qcontent", qcontent);
		setQuery(baseService.queryList("mywork.query", map));
		return SUCCESS;
	}
	/**
	 * 查询工作列表详细
	 * @return
	 * 2012-2-5 上午10:56:58
	 */
	public String detail() {
		String id = this.getRequest().getParameter("id");
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		String taskId = this.getRequest().getParameter("taskId");
		if(id.isEmpty()) {
			return SUCCESS;
		}
		String userId =  (String) baseService.queryOne("user.queryId", COMMITERCODE);
		Map<String,String> map = new HashMap<String,String>();
		map.put("ID", id);
		map.put("USERID", userId);
		detail = (Map) baseService.queryOne("mywork.detail",map);
		detail.put("BILLCODE", detail.get("CODE"));
		Map newMap = (Map) baseService.queryOne("businessProcess.queryByCode",detail);
		String processId = newMap == null ? "":(String)newMap.get("PROCESSID") ;
		detail.put("processInstanceId", processId);
		//detail.put("op", "review");
		//获取单位
		detail = agencyCoreService.agencyList(Constants.WORKENTRUST_BUSINESS_ID, userId,detail);
		if(null == taskId || taskId.isEmpty()) {
			return SUCCESS;
		}
		
		detail.put("taskId", taskId);
		List list =(taskTransitionService.getTaskTransition(taskId));
		Map url = new HashMap();
		StringBuffer bf = new StringBuffer();
		bf.append("/mywork/mywork_bxSubmit.action?id=");
		bf.append(id);
		bf.append("&COMMITERCODE=");
		bf.append(COMMITERCODE);
		bf.append("&taskId=");
		bf.append(taskId);
		url.put("url", bf.toString());
		list.add(url);
		detail.put("button", list);
		return SUCCESS;
	}
	
	public String bxSubmit() {
		//获取ID
		String id = this.getRequest().getParameter("id");
		//用户代号
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if(id.isEmpty() || COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		//任务唯一标识taskId
		String taskId =  this.getRequest().getParameter("taskId");
		//处理意见
		String commentContent =  this.getRequest().getParameter("comment");
		try {
			String s = new String(commentContent.getBytes("iso-8859-1"),"utf-8");
			commentContent = java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}

		//流向
		String transition = this.getRequest().getParameter("name");
		//处理类型
		String typeaccept = this.getRequest().getParameter("pass");
		
		Map param = new HashMap();
		param.put("COMMITERCODE",COMMITERCODE );
		param.put("ID", id);
		param.put("taskId", taskId);
		param.put("comment", commentContent);
		param.put("transition",transition );
		param.put("pass", typeaccept);
		Map mapUuid = (Map) baseService.queryOne("user.queryAll", COMMITERCODE);
		param.put("uuitId", mapUuid.get("ORGID"));
		bxSubmit = workEntrustService.bxSubmit(param);
		return SUCCESS;
	}

	
}
